import 'package:app/common/index.dart';
import 'package:app/models/profile.dart';
import 'package:app/models/user.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

// 封装一个基础的ChangeNotifier类，且重写notifyListeners方法(以便于更新的时候进行持久化)
class BaseChangeNotifier extends ChangeNotifier {
  Profile get _profile => Global.profile;

  @override
  void notifyListeners() {
    Global.saveProfile(); // 状态有更新的时候进行持久化
    super.notifyListeners();
  }
}

// 用户状态模块
class UserModel extends BaseChangeNotifier {
  String? get token => _profile.token;
  UserInfo? get userInfo => _profile.userInfo;

  set token(String? value) {
    _profile.token = value;
    notifyListeners();
  }

  set userInfo(UserInfo? value) {
    _profile.userInfo = value;
    notifyListeners();
  }

  set name(String? value) {
    _profile.userInfo?.name = value;
    notifyListeners();
  }

  set gender(int? value) {
    _profile.userInfo?.gender = value;
    notifyListeners();
  }

  set email(String? value) {
    _profile.userInfo?.email = value;
    notifyListeners();
  }

  set phoneNum(String? value) {
    _profile.userInfo?.phoneNum = value;
    notifyListeners();
  }

  set enterprise(String? value) {
    _profile.userInfo?.enterprise = value;
    notifyListeners();
  }

  set position(String? value) {
    _profile.userInfo?.position = value;
    notifyListeners();
  }
}

// 应用主题模块
class ThemeModel extends BaseChangeNotifier {
  ThemeMode get themeMode => _profile.themeMode ?? Global.defaultThemeMode;

  set themeMode(ThemeMode value) {
    _profile.themeMode = value;
    notifyListeners();
  }
}
